﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataModels.Common;

namespace GenericTools.Models
{
    public class AutoReminderSettings
    {
        #region Constants
        /// <summary>
        /// Default Setting: Contains the default reply language
        /// </summary>
        public const int DEFAULT_SYSTEM_REPLY_LANGUAGE = 1;

        /// <summary>
        /// Setting name: DefaultLang
        /// </summary>
        public const string REPLY_LANGUAGE = "DefaultLang";

        /// <summary>
        /// Default Setting: Contains the default message template
        /// </summary>
        public const string DEFAULT_MESSAGE_TEMPLATE = "Please send all PMTCT reports";

        /// <summary>
        /// Default Setting: Contains the number of days before sending reminders
        /// </summary>
        public const int DEFAULT_DAYS_BEFORE_REMINDING = 5;

        /// <summary>
        /// Default Setting: Enable SMS Reminders
        /// </summary>
        public const bool DEFAULT_ENABLE_SMS_REMINDERS = true;

        /// <summary>
        /// Default Setting: When this number of report is reached single reminder will be
        /// sent instead of several per report in the facility.
        /// </summary>
        public const int DEFAULT_REPORT_NUMBER_BEFORE_SINGLE_REMINDER = 3;

        /// <summary>
        /// Default Setting: Start of working hours
        /// </summary>
        public const string DEFAULT_START_WORKING_HOURS = "2000-01-01 07:30:00";

        /// <summary>
        /// Default Setting: End of working hours
        /// </summary>
        public const string DEFAULT_END_WORKING_HOURS = "2000-01-01 16:00:00";

        /// <summary>
        /// Default Setting: Times facilities have been reminded in a month
        /// </summary>
        public const int DEFAULT_TIMES_REMINDED_IN_MONTH = 0;

        /// <summary>
        /// Seting name: Enable Automatic SMS Reminders.
        /// </summary>
        public const string ENABLE_AUTO_SMS_REMINDERS = "Enable Automatic SMS Reminder";

        /// <summary>
        /// Setting name: Automatic SMS Reminder Basic information/configuration
        /// </summary>
        public const string AUTO_REMINDER_BASIC_INFO = "AutoReminder Basic Information";

        /// <summary>
        /// Setting name: Automatic SMS Reminder Message Template.
        /// Holds the flag to signal the means of getting the message templates.
        /// </summary>
        public const string MESSAGE_TEMPLATE = "AutoReminder Message Template";

        ///// <summary>
        ///// Setting name: Receivers of automatic SMS Reminders.
        ///// Holds the flags to signal the means of getting these message receivers
        ///// </summary>
        //public static readonly string RECEIVERS_OF_REMINDERS = "AutoReminder Receivers";

        ///// <summary>
        ///// Setting name: Holds the information of how to determine the pattern of facilities 
        ///// which will be reminded.
        ///// This may be by specifying facilities, districts or regions.
        ///// </summary>
        //public static readonly string REMINDING_FACILITIES_PATTERN = "AutoReminder Pattern of Reminding Facilities";

        /// <summary>
        /// Setting name: Holds the number of days passed before sending reminders
        /// </summary>
        public const string TRIGGER_DAYS_BEFORE_REMINDING = "AutoReminder Days passed before sending reminders";

        /// <summary>
        /// Setting name: Sending exception start working hours
        /// </summary>
        public const string SENDING_EXCEPTION_START_WORKING = "AutoReminder Sending Exception Start Working Hours";

        /// <summary>
        /// Setting name: Sending exception end working hours
        /// </summary>
        public const string SENDING_EXCEPTION_END_WORKING = "AutoReminder Sending Exception End Working Hours";

        /// <summary>
        /// Setting name: Message Template One Reminder per Facility Limit
        /// </summary>
        public const string MESSAGE_TEMPLATE_ONE_REMINDER_PER_FACILITY = "AutoReminder One Reminder Per Facility Limit";

        /// <summary>
        /// Setting name: AutoReminder Last save date
        /// </summary>
        public const string AUTO_REMINDER_SETTING_SAVE_DATE = "AutoReminder Last Save Date";

        /// <summary>
        /// Setting name: AutoReminder Reminding Month
        /// </summary>
        public const string AUTO_REMINDER_REMINDING_MONTH = "AutoReminder Reminding Month";

        /// <summary>
        /// Setting name: AutoReminder Times Reminded in Month
        /// </summary>
        public const string AUTO_REMINDER_TIMES_REMINDED_IN_MONTH = "AutoReminder Times Reminded in Month";

        /// <summary>
        /// Setting namem: AutoReminder Last Send Date
        /// </summary>
        public const string AUTO_REMINDER_LAST_SEND_DATE = "Auto Reminder Last Send Date";

        ///// <summary>
        ///// Setting name: Repetition pattern.
        ///// This can weekly or monthly
        ///// </summary>
        //public static readonly string REPETITION_PATTERN = "AutoReminder Repetition";

        ///// <summary>
        ///// Setting name: Stopping Condition.
        ///// This can be until the report is received, end of the month or more...
        ///// </summary>
        //public static readonly string STOPPING_CONDITION = "AutoReminder Stopping Condition";

        ///// <summary>
        ///// Setting name: Sending Pattern Exception.
        ///// Outline the flags that hold some exceptions during sending.
        ///// Thing like week-ends and holidays are dealt here.
        ///// </summary>
        //public static readonly string SENDING_PATTERN_EXCEPTION = "AutoReminder Sending Pattern Exception";
        #endregion

        static AutoReminderSettings()
        {
            // Enable automatic SMS Reminder
            string setting = Utilities.GetSetting(ENABLE_AUTO_SMS_REMINDERS);
            if (string.IsNullOrEmpty(setting)) Utilities.SetSetting(ENABLE_AUTO_SMS_REMINDERS, DEFAULT_ENABLE_SMS_REMINDERS);

            // Basic info
            setting = Utilities.GetSetting(AUTO_REMINDER_BASIC_INFO);
            if (string.IsNullOrEmpty(setting))
            {
                AutoReminderConfiguration config = AutoReminderConfiguration.FACTORY_SETTINGS;
                //config |= AutoReminderConfiguration.MESSAGE_TEMPLATE_SAME_ALL_REPORTS;
                //config |= AutoReminderConfiguration.REMINDER_RECEIVERS_ALL_REGISTERED;
                //config |= AutoReminderConfiguration.REMINDED_FACILITIES_BY_REGIONS;
                //config |= AutoReminderConfiguration.REPETITION_PATTERN_WEEKLY;
                //config |= AutoReminderConfiguration.STOPPING_CONDITION_UNTILL_END_OF_MONTH;
                //config |= AutoReminderConfiguration.SENDING_EXCEPTION_EXCLUDE_WEEK_ENDS;
                //config |= AutoReminderConfiguration.SENDING_EXCEPTION_EXCLUDE_HOLIDAYS;
                //config |= AutoReminderConfiguration.SENDING_EXCEPTION_USE_WORKING_HOURS;
                //config |= AutoReminderConfiguration.MESSAGE_TEMPLATE_ONE_REMINDER_PER_FACILITY;

                int iConfig = (int)config;
                Utilities.SetSetting(AUTO_REMINDER_BASIC_INFO, iConfig);
            }

            // Single Reminder Limit
            setting = Utilities.GetSetting(MESSAGE_TEMPLATE_ONE_REMINDER_PER_FACILITY);
            if (string.IsNullOrEmpty(setting))
            {
                Utilities.SetSetting(MESSAGE_TEMPLATE_ONE_REMINDER_PER_FACILITY,
                    DEFAULT_REPORT_NUMBER_BEFORE_SINGLE_REMINDER);
            }

            // Message Tempate
            setting = Utilities.GetSetting(MESSAGE_TEMPLATE);
            if (string.IsNullOrEmpty(setting)) Utilities.SetSetting(MESSAGE_TEMPLATE, DEFAULT_MESSAGE_TEMPLATE);

            // Days before reminding
            setting = Utilities.GetSetting(TRIGGER_DAYS_BEFORE_REMINDING);
            if (string.IsNullOrEmpty(setting)) Utilities.SetSetting(TRIGGER_DAYS_BEFORE_REMINDING, DEFAULT_DAYS_BEFORE_REMINDING);

            // Start of Working hours
            setting = Utilities.GetSetting(SENDING_EXCEPTION_START_WORKING);
            if (string.IsNullOrEmpty(setting))
            {
                DateTime startHour = DateTime.Parse(DEFAULT_START_WORKING_HOURS);
                string startWorkingHour = startHour.ToString("s");
                Utilities.SetSetting(SENDING_EXCEPTION_START_WORKING, startWorkingHour);
            }

            // End of working hours
            setting = Utilities.GetSetting(SENDING_EXCEPTION_END_WORKING);
            if (string.IsNullOrEmpty(setting))
            {
                DateTime endHour = DateTime.Parse(DEFAULT_END_WORKING_HOURS);
                string endWorkingHour = endHour.ToString("s");
                Utilities.SetSetting(SENDING_EXCEPTION_END_WORKING, endWorkingHour);
            }

        }

        public static bool TryLoadingDefaultConfigurations()
        {
            return true;
        }
    }
}
